import {IndexModel} from "../../../models/index.js";

const indexModel = new IndexModel()
const app = getApp()

Page({
  data: {
    TabCur: 0,
    scrollLeft: 0,
    navList:[
      { id: 0, title:"推荐"},
      { id: 1, title:"摄影师"},
      { id: 2, title:"模特"},
      { id: 3, title: "场地"}
      ],
    searching: false,
    hotWords:[],
    filterShow:false,
    imgUrls: [
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567577567245&di=e294fe3a7ca9e46ba65ec32dc69dc80d&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F23%2F20161023091149_hF8Hs.jpeg',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 300,
    recommendList:[],
    photographerList:[],
    modelList:[],
    placeList:[],
    params:{
      q:'',
      province:'全部',
      chargingModel:[],
      capacity:'',
      gender:'',
    },
  },
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  tabSelect(e) {
    const TabCur = e.currentTarget.dataset.id
    this.setData({
      TabCur: TabCur,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
    })
    const params = this.data.params
    if (TabCur != 3) {
      this.data.params.capacity = TabCur
    }
    if (this._beforeLoad(TabCur).length == 0) {
      this._Load(0);
    }
    this.setData({
      params
    })
  },
  handlerOnView: function (event) {
    let id = event.target.dataset.id
    wx.navigateTo({
      url: '../details/works/index?id='+id
    })
  },
  bindShowFilter: function () {
    this.setData({
      filterShow: !this.data.filterShow
    })
  },
  handlerClickMask: function () {
      this.setData({
          filterShow: false
      })
  },
  handlerFilterSubmit: function (event) {
    let params = event.detail.params
    this.setData({
      filterShow: false,
      params
    });
    this._Load(0)
  },
  handlerParentTap: function () {
    this.setData({
        filterShow: false
    })
  },
  onSearching: function() {
    this.setData({
      searching: true
    })
  },
  onCancel: function(event) {
    let params = this.data.params
    params.q = ''
    this.setData({
      searching: false,
      params
    })
    this._Load(0)
  },
  handlerRegion: function (event) {
    let value = event.detail.addressName
    let params = this.data.params
    params.province = value
    this.setData({
      params
    })
    this._Load(0)
  },
  handlerOnConfirm: function (event) {
    let q = event.detail.q
    let params = this.data.params
    params.q = q
    this.setData({
      searching: false,
      params
    })
    this._Load(0)
  },
  onDetails: function (event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../details/site/index?placeId='+id
    })
  },
  _loadWorksList(start,moreLoad) {
    const TabCur = this.data.TabCur
    indexModel.getUserWorks(this.data.params,start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this._setData(TabCur,res.data)
      }else {
        this._setData(TabCur,app._LoadMore(this._beforeLoad(TabCur),res.data))
        this.unLoading(res.data)
      }
      wx.hideLoading();
    })
  },
  _loadPlaceList(start,moreLoad) {
    indexModel.getPlaceList(this.data.params,start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          placeList: res.data
        })
      }else {
        this.setData({
          placeList: app._LoadMore(this.data.placeList,res.data)
        })
        this.unLoading(res.data)
      }
      wx.hideLoading();
    })
  },

  _Load(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    const tab = this.data.TabCur
    return tab == 3?this._loadPlaceList(start,moreLoad):this._loadWorksList(start,moreLoad)
  },
  _setData(TabCur,data) {
    switch (TabCur) {
      case 0:
        this.setData({
          recommendList: data
        })
        return
      case 1:
        this.setData({
          photographerList: data
        })
        return
      case 2:
        this.setData({
          modelList: data
        })
        return
      case 3:
        this.setData({
          placeList: data
        })
        return
    }
  },
  _beforeLoad(TabCur) {
    switch (TabCur) {
      case 0:
        return this.data.recommendList
      case 1:
        return this.data.photographerList
      case 2:
        return this.data.modelList
      case 3:
        return this.data.placeList
      default:
        return []
    }
  },
  _getHotList(topN) {
    indexModel.getHotWordList(topN).then(res=>{
      if (res.code == 0) {
        this.setData({
          hotWords:res.data
        })
      }
    })
  },
  _getIndexBack() {
    indexModel.getIndexBackList().then(res=>{
      if (res.code == 0 && res.data.length>0) {
        this.setData({
          imgUrls:res.data
        })
      }
    });
  },
  onLoad: function () {
    this._Load(0)
    this._getHotList(10)
    this._getIndexBack()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let _this = this
    wx.hideLoading();
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      _this._Load(0)
    },1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    _this._Load(_this._beforeLoad(_this.data.TabCur).length,true)
  },
  unLoading(data) {
    if (data.length == 0) {
      app.showInfo('没有更多了哦~')
    }
  },

})
